public class Test01 {
    public static String convert(String s, int numRows) {
        //处理边界条件
        if (numRows == 1) {
            return s;
        }
        //利用下标查找的规律
        //首先先求 公差
        int d = 2 * numRows - 2, n = s.length();
        StringBuilder ret = new StringBuilder();
        for (int i = 0; i < n; i += d) {
            ret.append(s.charAt(i));
        }
        //处理中间行
        for (int k = 1; k < numRows - 1; k++) {
            //两个两个相加
            //i < n || j < n 处理越界问题
            for (int i = k, j = d - i; i < n || j < n; i += d, j += d) {
                if (i < n) {
                    ret.append(s.charAt(i));
                }
                if (j < n) {
                    ret.append(s.charAt(j));
                }
            }
        }
        //处理最后一行
        for (int i = numRows - 1; i < n; i += d) {
            ret.append(s.charAt(i));
        }
        return ret.toString();
    }

    public static void main(String[] args) {
        System.out.println(convert("PAYPALISHIRING", 1));
    }
}
